package com.yunbiao.ybbusstopplate.base

import android.app.Application
import android.app.smdt.SmdtManager
import android.content.Context
//import androidx.multidex.MultiDex
import com.blankj.utilcode.util.Utils
import com.liulishuo.filedownloader.FileDownloader
import com.liulishuo.filedownloader.connection.FileDownloadUrlConnection
import com.lzy.okgo.OkGo
import com.tencent.upgrade.bean.UpgradeConfig
import com.tencent.upgrade.core.UpgradeManager
import com.umeng.commonsdk.UMConfigure
import com.umeng.umcrash.UMCrash
import com.yunbiao.ybbusstopplate.BuildConfig
import org.xutils.x


class APP : Application(){
    override fun onCreate() {
        super.onCreate()
        setAppContext(this)
        Utils.init(this)

        OkGo.getInstance().init(this);

        smdtManager= SmdtManager.create(this)
        //隐藏状态栏
//        if (smdtManager!=null){
//            smdtManager!!.smdtSetStatusBar(this,false)
//        }

        UMConfigure.setLogEnabled(true);
        UMCrash.enableLogBackup(true);
        UMCrash.enableKillProcessAfterCrash(true);
        //添加注释
        UMConfigure.init(this, "67f3ca4d48ac1b4f870ded55", "umeng", UMConfigure.DEVICE_TYPE_BOX, "");

        //初始化xutils 3.0
        x.Ext.init(this)
        //开启异常捕获
        if (ContentValue.openLog){
            cauchException()
        }

        FileDownloader.setupOnApplicationOnCreate(this)
            .connectionCreator(
                FileDownloadUrlConnection.Creator(
                    FileDownloadUrlConnection.Configuration()
                        .connectTimeout(15000) // set connection timeout.
                        .readTimeout(15000) // set read timeout.
                )
            )
            .commit()

//        MultiDex.install(this)

        val params = HashMap<String,String>()
        params["flavorType"] = BuildConfig.FLAVOR_TYPE.toString()
        val builder: UpgradeConfig.Builder = UpgradeConfig.Builder()
        val config: UpgradeConfig = builder
            .appId("6d44bf9323")
            .appKey("5beaac25-d7d6-4f3f-a30d-e46ed1be4de4")
            .customParams(params)
            .printInternalLog(true)
            .internalInitMMKVForRDelivery(true)
            .build()
        UpgradeManager.getInstance().init(this, config)
    }

    // -------------------异常捕获-----捕获异常后重启系统-----------------//
    private fun cauchException() { // 程序崩溃时触发线程
        Thread.setDefaultUncaughtExceptionHandler(MyUncaughtExceptionHandler(this))
    }

    companion object{
        private var appContext:Context?=null
        private var smdtManager:SmdtManager?=null


        fun setAppContext(context:Context?){
            appContext=context
        }

        fun getAppContext():Context?{
            return appContext
        }

        fun getSmdt():SmdtManager?{
            return smdtManager
        }

    }
}